package com.sf.shiva.trtms.ground.core.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.Random;

/**
 * 描述：生成 序列
 *
 * @author senbusi 2019/07/26
 * @version 1.0
 * @since 1.8
 */
public class SequenceGenerator {

    private static final Logger logger = LoggerFactory.getLogger(SequenceGenerator.class);

    private static final int IP;
    private static final int JVM = (int) (System.currentTimeMillis() >>> 8);
    private static final String SEP = "";

    private static short counter = (short) 1;

    private SequenceGenerator() {
    }

    public static int ipToInt(byte[] bytes) {
        int result = 0;
        for (int i = 0; i < 4; i++) {
            result = (result << 8) - Byte.MIN_VALUE + (int) bytes[i];
        }
        logger.info("ipToInt", result);
        return result;
    }

    static {
        int ipAddress;
        try {
            logger.info("This Server IP: {}", PlatformUtils.getLocalHostIp());
            ipAddress = ipToInt(PlatformUtils.getLocalHostIp().getBytes());
        } catch (Exception e) {
            logger.error("init ... Error!", e);
            ipAddress = 0;
        }
        IP = ipAddress;
    }

    public static int getJVM() {
        return JVM;
    }

    public static short getCount() {
        synchronized (SequenceGenerator.class) {
            if (counter < 0)
                counter = 0;
            return counter++;
        }
    }

    public static short getLimitCount() {
        synchronized (SequenceGenerator.class) {
            if (counter < 0 || counter > 9999) {
                counter = 0;
            }
            return counter++;
        }
    }

    public static short getHiTime() {
        return (short) (System.currentTimeMillis() >>> 32);
    }

    public static int getLoTime() {
        return (int) System.currentTimeMillis();
    }

    public static String format(int intValue) {
        String formatted = Integer.toHexString(intValue);
        StringBuilder buf = new StringBuilder("00000000");
        buf.replace(8 - formatted.length(), 8, formatted);
        return buf.toString();
    }

    public static String format(short shortValue) {
        String formatted = Integer.toHexString(shortValue);
        StringBuilder buf = new StringBuilder("0000");
        buf.replace(4 - formatted.length(), 4, formatted);
        return buf.toString();
    }

    public static String formatZero(short shortValue) {
        return String.format("%04d", shortValue);
    }

    public static String generate() {
        return format(getIp()) + SEP + format(getJVM()) + SEP + format(getHiTime()) + SEP + format(getLoTime()) + SEP + format(getCount());
    }

    public static String generate(String prefix) {
        return prefix + "" + format(getIp()).toUpperCase() + "" + DateUtils.format(DateUtils.Pattern.DATETIME_SHORT, DateUtils.currDateTime()) + formatZero(getLimitCount());
    }

    public static int getIp() {
        return IP;
    }

    /**
     * 按模块生成交易序号
     *
     * @param prefix    前缀
     * @param separator M-手机 T-pc
     * @param module    R-需求 C-竞价 T-招标
     * @return String code
     */
    public static String generateCode(String prefix, String separator, String module) {
        return prefix + "-" + separator + module + "-" + DateUtils.format(DateUtils.Pattern.DATETIME_FULL_SHORT, DateUtils.currDateTime())
                + (new Random().nextInt(900) + 100);
    }
}
